package cn.iocoder.ny.module.mall.service.impl;

import cn.iocoder.ny.module.mall.controller.admin.category.dto.MallProductByCategoryDTO;
import cn.iocoder.ny.module.mall.controller.admin.category.vo.MallProductByCategoryPageVo;
import cn.iocoder.ny.module.mall.controller.admin.category.vo.MallProductCategoryPageVo;
import cn.iocoder.ny.module.mall.controller.admin.category.vo.MallProductQueryCategoryVo;
import cn.iocoder.ny.module.mall.controller.admin.config.vo.MallConfigPageVo;
import cn.iocoder.ny.module.mall.domain.MallBanner;
import cn.iocoder.ny.module.mall.domain.MallBannerPageVo;
import cn.iocoder.ny.module.mall.domain.MallConfig;
import cn.iocoder.ny.module.mall.domain.MallProductCategory;
import cn.iocoder.ny.module.mall.domain.dto.MallProductMenuDTO;
import cn.iocoder.ny.module.mall.mapper.MallProductCategoryMapper;
import cn.iocoder.ny.module.mall.service.IMallProductCateGoryService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * @author luchunhong
 * @date 2024/07/23/ 15:00
 * @description:
 */
@Service
public class MallProductCateGoryServiceImpl extends ServiceImpl<MallProductCategoryMapper, MallProductCategory> implements IMallProductCateGoryService {

    @Autowired
    private MallProductCategoryMapper mallProductCategoryMapper;


//    @Override
//    public PageResult<MallBanner> getBannerPage(MallBannerPageVo mallBannerPageVo) {
//        return mallBannerMapper.selectPage(mallBannerPageVo);
//
//    }
    @Override
    public PageResult<MallProductCategory> queryList(MallProductCategoryPageVo mallProductCategory) {

        return mallProductCategoryMapper.selectPage(mallProductCategory);
    }

    @Override
    public MallProductCategory queryById(Long id) {
        return lambdaQuery().eq(MallProductCategory::getId, id).one();
    }

    @Override
    public boolean updateBatch(List<MallProductCategory> list) {

//        for (MallProductCategory mallProductCategory : list) {
//            MallProductCategory mallInfo = lambdaQuery().eq(MallProductCategory::getId, mallProductCategory.getId()).select().one();
//                Long count = lambdaQuery().eq(MallProductCategory::getParentId, mallInfo.getId()).eq(MallProductCategory::getDelFlag,"0").count();
//                if (count > 0){
//                    return false;
//                }
//        }
        return updateBatchById(list);
    }
    @Override
    public List<MallProductMenuDTO> menuSearch(MallProductMenuDTO mallProductMenuDTO) {
        // 查询所有符合条件的菜单
        List<MallProductMenuDTO> mallDto = mallProductCategoryMapper.queryMenuList(mallProductMenuDTO);

        // 筛选顶级菜单
        List<MallProductMenuDTO> rootMenus = new ArrayList<>();
        for (MallProductMenuDTO menu : mallDto) {
            if (menu.getParentId() == 0) {  // 只添加顶级菜单
                rootMenus.add(menu);
            }else if (!ObjectUtils.isEmpty(mallProductMenuDTO.getParentId()) && mallProductMenuDTO.getParentId() != 0 ){
                return mallDto;
            }
        }

        // 构建子菜单树
        for (MallProductMenuDTO rootMenu : rootMenus) {
            List<MallProductMenuDTO> children = getChild(rootMenu.getId(), mallDto);
            rootMenu.setSubCategories(children);
        }

        return rootMenus;
    }




    @Override
    public List<MallProductCategory> queryFirstMenu(MallProductCategory mallProductCategory) {
        List<MallProductCategory> list = lambdaQuery()
                .eq(MallProductCategory::getParentId, 0)
                .eq(MallProductCategory::getDelFlag,0)
                .eq(mallProductCategory.getTenantId() != null,MallProductCategory::getTenantId,mallProductCategory.getTenantId()).list();
        return list;
    }

//    @Override
//    public PageResult<MallConfig> getConfigPage(MallConfigPageVo reqVO) {
//        IPage<MallConfig> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
//        mallConfigMapper.queryPage(page, reqVO);
//        return new PageResult<>(page.getRecords(),page.getTotal());
//    }

    /**
     * 分页查询
     * @param mallProductByCategoryPageVo
     * @return
     */
    @Override
    public PageResult<MallProductByCategoryDTO> queryProductListByCategoryId(MallProductByCategoryPageVo mallProductByCategoryPageVo)
    {
        IPage<MallProductByCategoryDTO> page = new Page<>(mallProductByCategoryPageVo.getPageNo(), mallProductByCategoryPageVo.getPageSize());
        mallProductCategoryMapper.queryProductInfoByCategory(page,mallProductByCategoryPageVo);
        return new PageResult<>(page.getRecords(),page.getTotal());
    }
    @Override
    public MallProductQueryCategoryVo queryCategoryParent(Long parentId) {
        MallProductQueryCategoryVo mallProductQueryCategoryVo = new MallProductQueryCategoryVo();
        if (parentId != 0){
            MallProductCategory mallProductCategory = lambdaQuery().eq(MallProductCategory::getId, parentId).one();
            mallProductQueryCategoryVo.setId(Long.valueOf(mallProductCategory.getId()));
            mallProductQueryCategoryVo.setParentId(Long.valueOf(mallProductCategory.getParentId()));
            mallProductQueryCategoryVo.setName(mallProductCategory.getName());
        }else {
            mallProductQueryCategoryVo.setName("主类目");
            mallProductQueryCategoryVo.setParentId(0L);
            mallProductQueryCategoryVo.setId(0L);
        }
        return mallProductQueryCategoryVo;
    }

    /**
     * 保存分类
     * @param mallProductCategory
     * @return
     */
    @Override
    public boolean saveProductCategory(MallProductCategory mallProductCategory) {
        Long count = lambdaQuery().eq(!ObjectUtils.isEmpty(mallProductCategory.getName()), MallProductCategory::getName, mallProductCategory.getName())
                .eq(!ObjectUtils.isEmpty(mallProductCategory.getTenantId()), MallProductCategory::getTenantId, mallProductCategory.getTenantId())
                .eq(MallProductCategory::getDelFlag,0).count();
        if (count == 0){
            LoginUser loginUser = getLoginUser();
            mallProductCategory.setTenantId(loginUser.getTenantId());
            save(mallProductCategory);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateProductCategoryById(MallProductCategory mallProductCategory) {
        Long count = lambdaQuery()
                .eq(!ObjectUtils.isEmpty(mallProductCategory.getName()), MallProductCategory::getName, mallProductCategory.getName())
                .eq(!ObjectUtils.isEmpty(mallProductCategory.getTenantId()), MallProductCategory::getTenantId, mallProductCategory.getTenantId())
                .eq(MallProductCategory::getDelFlag,0)
                .ne(MallProductCategory::getId, mallProductCategory.getId()).count();
        if (count == 0){
            updateById(mallProductCategory);
            return true;
        }
        return false;
    }



    private List<MallProductMenuDTO> getChild(Long id, List<MallProductMenuDTO> mallDto) {
        List<MallProductMenuDTO> childList = new ArrayList<>();
        for (MallProductMenuDTO menu : mallDto) {
            if (Objects.equals(menu.getParentId(), id)) {
                childList.add(menu);
            }
        }

        // 递归获取子菜单
        for (MallProductMenuDTO child : childList) {
            List<MallProductMenuDTO> grandChildren = getChild(child.getId(), mallDto);
            child.setSubCategories(grandChildren);
        }

        return childList; // 返回的是空列表
    }


    }


//    private List<MallProductMenuDTO> getChild(Long id, List<MallProductMenuDTO> mallDto) {
//        List<MallProductMenuDTO> childList = new ArrayList<>();
//        for (MallProductMenuDTO menu : mallDto) {
//            if (menu.getParentId().equals(id)){
//                childList.add(menu);
//            }
//
//        }
//        for (MallProductMenuDTO mallProductCategory : childList) {
//            List<MallProductMenuDTO> child = getChild(Long.valueOf(mallProductCategory.getId()), mallDto);
//            mallProductCategory.setSubCategories(child);
//        }
//        if(childList.size()==0){
//            return  null;
//        }
//        return childList;
//    }


